package com.example.codefilter.framework.security.handle;

import com.alibaba.fastjson2.JSON;
import com.example.codefilter.common.core.AjaxResult;
import com.example.codefilter.common.util.ServletUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import java.io.IOException;

/**
 * @version 1.0.0
 * @className: LogoutSuccessHandlerImpl
 * @description: 退出处理器
 * @author: LiJunYi
 * @create: 2022/7/26 8:34
 */
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
{

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication e) throws IOException {
        boolean isAjaxRequest = ServletUtils.isAjaxRequest(request);
        if (isAjaxRequest)
        {
            ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.success("退出成功")));
        }else
        {
            response.sendRedirect("/login");
        }
    }
}
